//
//  CompleteVC.m
//  Zhiche-Carriage
//
//  Created by LeeBruce on 16/6/20.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "CompleteVC.h"
#import "TopBackView.h"
#import "CarChoiceVC.h"
#import "CarSizeVC.h"
#import "QiniuSDK.h"
#import "Common.h"
#import "PictureCollectionCell.h"


@interface CompleteVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong) UITextField *nameField;
@property (nonatomic,strong) UITextField *qianNumberField;
@property (nonatomic,strong) UITextField *guaNumberField;

@property (nonatomic,strong) UIButton *carStyleButton;
@property (nonatomic,strong) UIButton *carSizeButton;


@property (nonatomic,copy) NSString *token;
@property (nonatomic,copy) NSString *qiNiuKey;
@property (nonatomic,strong) UIImageView *imagev;
@property (nonatomic,strong) NSIndexPath *indexPath;

@property (nonatomic,strong) NSMutableDictionary *postDic;
@property (nonatomic,strong) NSMutableDictionary *dataDictionary;
@property (nonatomic,strong) NSMutableArray *postArray;



@end

@implementation CompleteVC

- (void)viewDidLoad {
    [super viewDidLoad];
    TopBackView *topBackView= [[TopBackView alloc]initViewWithFrame:CGRectMake(0, 0, screenWidth, 64) andTitle:@"车辆信息完善"];
    self.dataDictionary = [NSMutableDictionary dictionary];
    
    self.postDic = [NSMutableDictionary dictionary];
    self.postArray = [NSMutableArray array];
    
    topBackView.rightButton.hidden = YES;
    [topBackView.leftButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:topBackView];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.postDic setValue:@"" forKey:@"tractorNumber"];//牵引车车牌号
    [self.postDic setValue:@"" forKey:@"trailerNumber"];//挂车车牌号
    [self.postDic setValue:@"" forKey:@"lengthCode"];//车长编码
    
    [self.postDic setValue:@"" forKey:@"lengthDesc"];//车长描述
    [self.postDic setValue:@"" forKey:@"widthCode"];//车宽编码
    [self.postDic setValue:@"" forKey:@"widthDesc"];//车宽描述
    [self.postDic setValue:@"" forKey:@"heightCode"];//车高编码
    [self.postDic setValue:@"" forKey:@"heightDesc"];//车高描述
    [self.postDic setValue:@"" forKey:@"typeCode"];//车辆种类编码
    [self.postDic setValue:@"" forKey:@"typeDesc"];//车辆种类描述
    [self.postDic setValue:@"" forKey:@"shaftTypeCode"];//轴数编码
    [self.postDic setValue:@"" forKey:@"shaftTypeDesc"];//轴数描述
    [self.postDic setValue:@"" forKey:@"trailerTypeCode"];//挂车类型编码
    [self.postDic setValue:@"" forKey:@"trailerTypeDesc"];//挂车类型描述
    [self.postDic setValue:@"" forKey:@"boxTypeCode"];//箱体结构编码
    [self.postDic setValue:@"" forKey:@"boxTypeDesc"];//箱体结构描述
    [self.postDic setValue:@"" forKey:@"storeNumberDesc"];//仓位数描述
    [self.postDic setValue:@"" forKey:@"storeNumberCode"];//仓位数编码
    [self.postDic setValue:@"" forKey:@"truckAnnex"];
    [self.postDic setValue:@"" forKey:@"id"];//ID主键
    [self.postDic setObject:@"" forKey:@"userName"];
    
    [self initDataSource];
    
//        [self initSubviews];
    
}

-(void)initDataSource
{
    
    NSString *string = [NSString stringWithFormat:@"%@",find_truckinfo];
    
    [Common requestWithUrlString:string contentType:application_json finished:^(id responseObj) {
        
        NSLog(@"%@",responseObj);
        
       
        self.dataDictionary = [NSMutableDictionary dictionaryWithDictionary:responseObj[@"data"]];
        
        if ([responseObj[@"success"] boolValue]) {
            
            [self initSubviews];
            
                        [self giveValueStringWithDic:self.dataDictionary];
            
            [self.collectionView reloadData];
        }
        
        
    } failed:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];
    
    
}

#pragma mark 赋值
-(void)giveValueStringWithDic:(NSMutableDictionary *)dic
{
    
    //真实姓名
    self.nameField.text = [NSString stringWithFormat:@"%@",dic[@"user"][@"realName"]];
    //牵引车车牌号
    self.qianNumberField.text = [NSString stringWithFormat:@"%@",dic[@"tractorNumber"]];
    //挂车车牌号
    self.guaNumberField.text = [NSString stringWithFormat:@"%@",dic[@"trailerNumber"]];
    // 车辆类型
    
    
    
    NSString *styleS = [NSString stringWithFormat:@"%@ %@ %@ %@  %@",dic[@"typeDesc"],dic[@"shaftTypeDesc"],dic[@"trailerTypeDesc"],dic[@"boxTypeDesc"],dic[@"storeNumberDesc"]];
    
    [self.carStyleButton setTitle:styleS forState:UIControlStateNormal];
    
    // 长 宽 高
    
    NSString *sizeS = [NSString stringWithFormat:@"%@ %@ %@",dic[@"lengthDesc"],dic[@"widthDesc"],dic[@"heightDesc"]];
    
    [self.carSizeButton setTitle:sizeS forState:UIControlStateNormal];
    
    
}


-(void)initSubviews
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight - 64)];
    [self.view addSubview:self.scrollView];
    
    self.scrollView.contentSize = CGSizeMake(screenWidth, screenHeight * 1.5);
    
    [self initTextField];
    
    //基本信息
    UIView *firstV = [self backBackgroundViewWithString:@"基本信息" andFrame:CGRectMake(0, 0, screenWidth, 4 * cellHeight)];
    
    [self.scrollView addSubview:firstV];
    //真实姓名
    UIView *nameV = [self backViewWithFrame:CGRectMake(0, cellHeight, screenWidth, cellHeight) andString:@"真实姓名:" andField:self.nameField];
    [firstV addSubview:nameV];
    
    //牵引车车牌号
    UIView *qianV = [self backViewWithFrame:CGRectMake(0, cellHeight * 2, screenWidth, cellHeight) andString:@"牵引车车牌号:" andField:self.qianNumberField];
    [firstV addSubview:qianV];
    //挂车车牌号
    UIView *guaV = [self backViewWithFrame:CGRectMake(0, cellHeight * 3, screenWidth, cellHeight) andString:@"挂车车牌号:" andField:self.guaNumberField];
    [firstV addSubview:guaV];
    
    
    //车辆类型
    UIView *secondV = [self backBackgroundViewWithString:@"车辆类型" andFrame:CGRectMake(0, CGRectGetMaxY(firstV.frame), screenWidth, cellHeight * 2)];
    
    UIView *whiteV = [[UIView alloc]initWithFrame:CGRectMake(0, cellHeight, screenWidth, cellHeight)];
    whiteV.backgroundColor = WhiteColor;
    
    
    self.carStyleButton = [self backButton];
    [whiteV addSubview:self.carStyleButton];
    [secondV addSubview:whiteV];
    
    [self.carStyleButton addTarget:self action:@selector(carStyleButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.scrollView addSubview:secondV];
    
    //车身尺寸
    UIView *thirdV = [self backBackgroundViewWithString:@"车身尺寸" andFrame:CGRectMake(0, CGRectGetMaxY(secondV.frame), screenWidth, cellHeight * 2)];
    UIView *whiteV1 = [[UIView alloc]initWithFrame:CGRectMake(0, cellHeight, screenWidth, cellHeight)];
    whiteV1.backgroundColor = WhiteColor;
    
    self.carSizeButton = [self backButton];
    [whiteV1 addSubview:self.carSizeButton];
    [thirdV addSubview:whiteV1];
    
    [self.carSizeButton addTarget:self action:@selector(carSizeButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.scrollView addSubview:thirdV];
    
    [self initCollectionView];
    
    
}

-(void)initTextField
{
    self.nameField = [[UITextField alloc]init];
    self.qianNumberField = [[UITextField alloc]init];
    self.guaNumberField = [[UITextField alloc]init];
    
    
    self.nameField.borderStyle = UITextBorderStyleRoundedRect;
    self.qianNumberField.borderStyle = UITextBorderStyleRoundedRect;
    self.guaNumberField.borderStyle = UITextBorderStyleRoundedRect;
    
}


-(void)initCollectionView
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(80 * kWidth, 80 * kHeight);
    layout.minimumInteritemSpacing = 0;
    
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, cellHeight * 8, screenWidth, 3 * 120 * kHeight) collectionViewLayout:layout];
    
    
    [self.scrollView addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = WhiteColor;
    
    [self.collectionView registerClass:[PictureCollectionCell class] forCellWithReuseIdentifier:@"carInfoCell"];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
    [self initBottomButton];
    
}


#pragma mark  UICollectionViewDelegate,UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    if (section == 0) {
        return  [self.dataDictionary[@"truckPicMap"][@"item"] count];
    } else
        
        if (section == 1) {
            return  [self.dataDictionary[@"licensePicMap"][@"item"] count];
            
        } else
            
        {
            
            return  [self.dataDictionary[@"insurePicMap"][@"item"] count];
            
        }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellIden = @"carInfoCell";
    PictureCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIden forIndexPath:indexPath];
    
    cell.ImageView.backgroundColor = YellowColor;
    if (indexPath.section == 0) {
        
        cell.label.text = [NSString stringWithFormat:@"%@",self.dataDictionary[@"truckPicMap"][@"item"][indexPath.row][@"picDesc"]];
        
    }
    
    if (indexPath.section == 1) {
        
        cell.label.text = [NSString stringWithFormat:@"%@",self.dataDictionary[@"licensePicMap"][@"item"][indexPath.row][@"picDesc"]];
    }
    
    if (indexPath.section == 2) {
        cell.label.text = [NSString stringWithFormat:@"%@",self.dataDictionary[@"insurePicMap"][@"item"][indexPath.row][@"picDesc"]];
    }
    
    return cell;
    
}
//头标题尺寸
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(screenWidth, 30 * kHeight);
}
//items的偏移量
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 14, 5, 14);
}

//头标题
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
    
    for (UILabel *view in headerView.subviews) {
        [view removeFromSuperview];
    }
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(14, 0, 100, headerView.frame.size.height)];
    lable.font = Font(15);
    NSArray *array = @[self.dataDictionary[@"truckPicMap"][@"name"],self.dataDictionary[@"licensePicMap"][@"name"],self.dataDictionary[@"insurePicMap"][@"name"]];
    
    lable.text = array[indexPath.section];
    
    [headerView addSubview:lable];
    headerView.backgroundColor = GrayColor;
    
    
    return headerView;
}

////间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 10;
//}
////- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 10;
//}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.indexPath = indexPath;
    [self photoButton];
    
}


-(void)initBottomButton{
    
    UIButton *bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    bottomButton.frame = CGRectMake(20, CGRectGetMaxY(self.collectionView.frame) + 20, screenWidth - 40, 35);
    [bottomButton setTitle:@"确认" forState:UIControlStateNormal];
    [bottomButton setTitleColor:BlackColor forState:UIControlStateNormal];
    bottomButton.titleLabel.font = Font(16);
    bottomButton.backgroundColor = YellowColor;
    bottomButton.layer.cornerRadius = 5;
    [bottomButton addTarget:self action:@selector(confirmButton) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:bottomButton];
    
    
}

-(void)confirmButton
{
    
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:self.postArray options:NSJSONWritingPrettyPrinted error:nil];
        NSString * jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    [self.postDic setObject:self.nameField.text forKey:@"userName"];
    [self.postDic setObject:self.qianNumberField.text forKey:@"tractorNumber"];
    [self.postDic setObject:self.guaNumberField.text  forKey:@"trailerNumber"];
    [self.postDic setObject:jsonString forKey:@"truckAnnex"];
    
    
    NSString *idS ;
    
    if ([self.dataDictionary[@"id"] isEqual:[NSNull null]]) {
        idS = @"";
    } else {
        idS = self.dataDictionary[@"id"];
    }
    
    [self.postDic setObject:idS forKey:@"id"];
    
    
    Common *c = [[Common alloc]init];
    [c afPostRequestWithUrlString:truckinfo_add parms:self.postDic finishedBlock:^(id responseObj) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:nil];
        
        [self showAlertWithString:dic[@"message"]];
        
        
    } failedBlock:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];
    
}


-(void)showAlertWithString:(NSString *)string
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1   = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *action2   = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action1];
    [alert addAction:action2];
    
    
    [self presentViewController:alert animated:YES completion:nil];
    
}


-(UIView *)backBackgroundViewWithString:(NSString *)string andFrame:(CGRect)frame
{
    UIView *view = [[UIView alloc]initWithFrame:frame];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(14, 0, 100, cellHeight)];
    label.font =Font(15);
    label.text = string;
    [view addSubview:label];
    
    view.backgroundColor = GrayColor;
    
    return view;
}



-(UIView *)backViewWithFrame:(CGRect)frame andString:(NSString *)string andField:(UITextField *)field
{
    UIView *view = [[UIView alloc]initWithFrame:frame];
    view.backgroundColor = WhiteColor;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(14, 0, 100, cellHeight)];
    label.font = Font(15);
    label.text = string;
    [view addSubview:label];
    
    
    field.frame = CGRectMake(CGRectGetMaxX(label.frame), 5, 185 * kWidth, cellHeight - 10);
    field.font = Font(15);
    [view addSubview:field];
    field.delegate = self;
    
    
    
    return view;
}

-(UIButton *)backButton
{
    UIButton *bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    bottomButton.frame = CGRectMake(20,0, screenWidth - 40, cellHeight);
    [bottomButton setTitleColor:BlackColor forState:UIControlStateNormal];
    bottomButton.titleLabel.font = Font(15);
    bottomButton.backgroundColor = WhiteColor;
    bottomButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    return bottomButton;
    
}
#pragma mark button方法
//车辆类型
-(void)carStyleButton:(UIButton *)sender
{
    
    CarChoiceVC *carVC = [[CarChoiceVC alloc]init];
    
    carVC.backValueString = ^(NSArray *arr)
    {
        
        
        [self.postDic setValue:arr[0] forKey:@"storeNumberDesc"];
        [self.postDic setValue:arr[1] forKey:@"storeNumberCode"];
        [self.postDic setValue:arr[2] forKey:@"trailerTypeDesc"];
        
        [self.postDic setValue:arr[3] forKey:@"trailerTypeCode"];
        [self.postDic setValue:arr[4] forKey:@"boxTypeDesc"];
        [self.postDic setValue:arr[5] forKey:@"boxTypeCode"];
        [self.postDic setValue:arr[6] forKey:@"typeDesc"];
        [self.postDic setValue:arr[7] forKey:@"typeCode"];
        [self.postDic setValue:arr[8] forKey:@"shaftTypeDesc"];
        [self.postDic setValue:arr[9] forKey:@"shaftTypeCode"];
        
        
        NSString *string = [NSString stringWithFormat:@"%@  %@  %@  %@  %@",arr[0],arr[2],arr[4],arr[6],arr[8]];
        
        [self.carStyleButton setTitle:string forState:UIControlStateNormal];
        
    };
    
    
    
    [self.navigationController pushViewController:carVC animated:YES];
    NSLog(@"车身类型");
}
//车身尺寸
-(void)carSizeButton:(UIButton *)sender
{
    CarSizeVC *carVC = [[CarSizeVC alloc]init];
    
    carVC.backValueString = ^(NSArray *arr){
        [self.postDic setValue:arr[0] forKey:@"lengthDesc"];
        [self.postDic setValue:arr[1] forKey:@"lengthCode"];
        [self.postDic setValue:arr[2] forKey:@"widthDesc"];
        [self.postDic setValue:arr[3] forKey:@"widthCode"];
        [self.postDic setValue:arr[4] forKey:@"heightDesc"];
        [self.postDic setValue:arr[5] forKey:@"heightCode"];
        
        NSString *string = [NSString stringWithFormat:@"%@  %@  %@",arr[0],arr[2],arr[4]];
        
        [self.carSizeButton setTitle:string forState:UIControlStateNormal];
        
        
    };
    
    
    
    [self.navigationController pushViewController:carVC animated:YES];
    NSLog(@"车身尺寸");
    
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark 获取图片
-(void)photoButton
{
    __weak typeof(self) weakSelf = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [weakSelf getPictureFromCamera];
        
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf getPictureFromLibrary];
        
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    
    
    [self presentViewController:alert animated:YES completion:nil];
    
}

#pragma mark 拍照
-(void)getPictureFromCamera
{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark 从相册选取
-(void)getPictureFromLibrary
{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:nil];
    
}

#pragma mark 从七牛获取Token
-(void)getTokenFromQN
{
    //    NSString *string = [NSString stringWithFormat:@"%@qiniu/upload/ticket",Main_interface];
    
    [Common requestWithUrlString:qiniu_token contentType:application_json finished:^(id responseObj) {
        if ([responseObj[@"success"] boolValue]) {
            
            PictureCollectionCell *pictureCell = (PictureCollectionCell *)[self.collectionView cellForItemAtIndexPath:self.indexPath];
            
            
            self.token = responseObj[@"data"];
            [self uploadImageToQNWithData:[self getImageWith:pictureCell.ImageView.image]];
        }
    } failed:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];
    
    
}

-(NSData *)getImageWith:(UIImage *)image
{
    NSData *data = nil;
    if (UIImagePNGRepresentation(image)) {
        
        data = UIImageJPEGRepresentation(image, 0.5);
        
        
    } else {
        data = UIImagePNGRepresentation(image);
        
        
    }
    
    return data;
}


-(void)uploadImageToQNWithData:(NSData *)data
{
    
    QNUploadManager *upManager = [[QNUploadManager alloc]init];
    
    QNUploadOption *uploadOption = [[QNUploadOption alloc]initWithMime:nil progressHandler:^(NSString *key, float percent) {
        
    } params:nil checkCrc:NO cancellationSignal:nil];
    
    [upManager putData:data key:nil token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        
        
        
        NSDictionary *dic = [NSDictionary dictionary];
        self.qiNiuKey = resp[@"key"];
        
        NSString *string1 ;
        NSString *string2 ;
        NSString *string3 ;

        
        if (self.indexPath.section == 0) {
            
            if ([self.dataDictionary[@"truckPicMap"][@"item"][self.indexPath.row][@"id"] isEqual:[NSNull null]]  ) {
                string1 = @"";
            } else {
                string1 = [NSString stringWithFormat:@"%@",self.dataDictionary[@"truckPicMap"][@"item"][self.indexPath.row][@"id"]];
            }
            
            
            dic = @{@"id": string1 ,
                    @"picType":[NSString stringWithFormat:@"%@",self.dataDictionary[@"truckPicMap"][@"item"][self.indexPath.row][@"picType"]],
                    @"picItem":[NSString stringWithFormat:@"%@",self.dataDictionary[@"truckPicMap"][@"item"][self.indexPath.row][@"picItem"]],
                    @"picKey":resp[@"key"],
                    @"picDesc":[NSString stringWithFormat:@"%@",self.dataDictionary[@"truckPicMap"][@"item"][self.indexPath.row][@"picDesc"]]};
            
            
        }
        
        if (self.indexPath.section == 2) {
            
            if ([self.dataDictionary[@"insurePicMap"][@"item"][self.indexPath.row][@"id"] isEqual:[NSNull null]]  ) {
                string2 = @"";
            } else {
                string2 = [NSString stringWithFormat:@"%@",self.dataDictionary[@"insurePicMap"][@"item"][self.indexPath.row][@"id"]];
            }

            
            
            dic = @{@"id": string2 ,
                    @"picType":[NSString stringWithFormat:@"%@",self.dataDictionary[@"insurePicMap"][@"item"][self.indexPath.row][@"picType"]],
                    @"picItem":[NSString stringWithFormat:@"%@",self.dataDictionary[@"insurePicMap"][@"item"][self.indexPath.row][@"picItem"]],
                    @"picKey":resp[@"key"],
                    @"picDesc":[NSString stringWithFormat:@"%@",self.dataDictionary[@"insurePicMap"][@"item"][self.indexPath.row][@"picDesc"]]};
        }
        
        if (self.indexPath.section == 1) {
            
            if ([self.dataDictionary[@"licensePicMap"][@"item"][self.indexPath.row][@"id"] isEqual:[NSNull null]]  ) {
                string3 = @"";
            } else {
                string3 = [NSString stringWithFormat:@"%@",self.dataDictionary[@"licensePicMap"][@"item"][self.indexPath.row][@"id"]];
            }
            

            
            
            dic = @{@"id": string3,
                    @"picType":[NSString stringWithFormat:@"%@",self.dataDictionary[@"licensePicMap"][@"item"][self.indexPath.row][@"picType"]],
                    @"picItem":[NSString stringWithFormat:@"%@",self.dataDictionary[@"licensePicMap"][@"item"][self.indexPath.row][@"picItem"]],
                    @"picKey":resp[@"key"],
                    @"picDesc":[NSString stringWithFormat:@"%@",self.dataDictionary[@"licensePicMap"][@"item"][self.indexPath.row][@"picDesc"]]};
        }
        
        
        if (self.postArray.count > 0) {
            for (int i = 0; i < self.postArray.count; i++) {
                
                if ([self.postArray[i][@"picItem"] isEqualToString:dic[@"picItem"]]) {
                    
                    [self.postArray removeObjectAtIndex:i];
                    [self.postArray addObject:dic];
                    
                } else {
                    [self.postArray addObject:dic];

                }
            }
            
            
        } else {
            
            [self.postArray addObject:dic];
            
        }
        
        
    } option:uploadOption];
}


#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        
        PictureCollectionCell *pictureCell = (PictureCollectionCell *)[self.collectionView cellForItemAtIndexPath:self.indexPath];
        
        pictureCell.ImageView.image = image;
        
        //        self.imagev.image = image;
        
        //上传服务器
        [self getTokenFromQN];
        
    }];
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self.view endEditing:NO];
}

//点击return 按钮 去掉
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
